<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
<body style="">
<!--信息表-->
<table id="role" lay-filter="roleTableFilter">
</table>
<!--头部工具栏按钮-->
<script type="text/html" id="roleToolbar">
    <div class="layui-btn-container">
        <button shiro:hasPermission="system:role:add" class="layui-btn layui-btn-xs" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
    </div>
</script>
<!--操作按钮-->
<script type="text/html" id="roleOperation">
    <button shiro:hasPermission="system:role:edit" class="layui-btn layui-btn-xs" lay-event="update">修改</button>
    <button  shiro:hasPermission="system:role:remove" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
</script>
<!--添加，弹窗表格-->
<form class="layui-form" action="" id="addRoleForm" style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" placeholder="请输入角色名称" required="" lay-verify="required|roleName"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色关键字</label>
        <div class="layui-input-block">
            <input type="text" name="roleKey" placeholder="请输入角色关键字" required="" lay-verify="required|roleKey"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="roleSort" placeholder="请输入排序" required="" lay-verify="required"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchTest" lay-text="正常|失效">
            <div class="layui-unselect layui-form-switch layui-form-onswitch" lay-skin="_switch"><em>正常</em><i></i>
            </div>
        </div>
    </div>
    <input hidden="" name="createBy">
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" placeholder="请输入备注" required="" lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <!--树形菜单-->
    <div class="layui-form-item">
        <label class="layui-form-label" style="float: left">菜单权限</label>
        <div style="margin-left: 120px">
            <div id="addRoleTree" class="demo-tree-more">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit id="addRoleButton" lay-filter="addRoleFilter" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--修改，弹窗表格-->
<form class="layui-form" action="" id="updateRoleForm" lay-filter="updateRoleFormFilter"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <input hidden="" name="roleId">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" placeholder="请输入角色名称" required="" lay-verify="required"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色关键字</label>
        <div class="layui-input-block">
            <input type="text" name="roleKey" placeholder="请输入角色关键字" required="" lay-verify="required"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="roleSort" placeholder="请输入排序" required="" lay-verify="required"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" id="updateStatus" lay-skin="switch" lay-filter="switchTest"
                   lay-text="正常|失效">
            <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>失效</em><i></i></div>
        </div>
    </div>
    <input hidden="" name="updateBy">
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" placeholder="请输入备注" required="" lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <!--树形菜单-->
    <div class="layui-form-item">
        <label class="layui-form-label" style="float: left">菜单权限</label>
        <div style="margin-left: 120px">
            <div id="updateRoleTree" class="demo-tree-more">

            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="updateRoleFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--状态按钮-->
<script type="text/html" id="roleStatus">
    {{# if (d.status === '0'){}}
    <button class="layui-btn layui-btn-xs" lay-event="state">正常</button>
    {{# }else {}}
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="state">失效</button>
    {{# }}}
</script>
<script>
    layui.use(['table', 'layer', 'jquery', 'form', 'tree', 'util'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;
        let tree = layui.tree
        let util = layui.util

        //判断关键字是否存在
        form.verify({
            roleName:function (value){
                let flag = true;
                $.ajax({
                    url:"/role/checkRoleName",
                    data:{"roleName":value},
                    async:false,
                    success:function (obj){
                        //数据库不存在该用户
                        if(obj.code === 0){
                            flag = false;
                        }
                    }
                });
                if(flag){
                    return "角色名称已存在";
                }
            },
            roleKey:function (value){
                let flag = true;
                $.ajax({
                    url:"/role/checkRoleKey",
                    data:{"roleKey":value},
                    async:false,
                    success:function (obj){
                        //数据库不存在该用户
                        if(obj.code === 0){
                            flag = false;
                        }
                    }
                });
                if(flag){
                    return "关键字已存在";
                }
            }
        })
        //第一个实例
        let roleTable = table.render({
            elem: '#role'
            , url: '/role/selectAll' //数据接口
            , toolbar: '#roleToolbar' //开启头部工具栏，并为其绑定左侧模板
            , page: true //开启分页
            , cols: [
                [ //表头
                    {field: 'roleId', title: '编号', width: 80, sort: true}
                    , {field: 'roleName', title: '名称', width: 100}
                    , {field: 'roleKey', title: '角色关键字', width: 120}
                    , {field: 'roleSort', title: '显示顺序', width: 160, sort: true}
                    , {field: 'status', title: '状态', width: 160, toolbar: "#roleStatus"}
                    , {field: 'createBy', title: '创建人', width: 100}
                    , {field: 'createTime', title: '创建时间', width: 160}
                    , {field: 'updateBy', title: '修改人', width: 100}
                    , {field: 'updateTime', title: '修改时间', width: 160}
                    //添加按钮
                    , {fixed: 'right', align: 'center', title: '操作', width: 220, toolbar: "#roleOperation"}
                ]
            ]
        });


        //头工具栏事件
        table.on('toolbar(roleTableFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data;
            switch (obj.event) {
                //添加数据
                case 'add':
                    //重新渲染表单
                    form.render();
                    layer.open({
                        title: '添加角色',
                        type: 1,
                        area: ['500px', '500px'],
                        content: $("#addRoleForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            }
        });


        //行工具栏事件
        table.on('tool(roleTableFilter)', function (obj) {
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'update') {
                console.log(data.status)
                if (data.status == 0) {
                    document.getElementById("updateStatus").checked = true
                } else {
                    document.getElementById("updateStatus").checked = false
                }
                form.val('updateRoleFormFilter', {
                    roleId: data.roleId
                    , roleName: data.roleName
                    , roleKey: data.roleKey
                    , roleSort: data.roleSort
                    , remark: data.remark
                });
                tree.reload('updateFormMenu')
                //树形菜单回显
                var ids = [];
                $.ajax({
                    url: "/role/findSelectMenu",
                    type: "GET",
                    dataType: "JSON",
                    data: {roleId: data.roleId},
                    success: function (ret) {
                        for (let i = 0; i < ret.data.length; i++) {
                            ids.push(ret.data[i].id);
                        }
                        //console.log(ids)
                        tree.setChecked('updateFormMenu', ids);
                    }
                })
                form.render()
                layer.open({
                    title: '修改角色',
                    type: 1,
                    area: ['500px', '500px'],
                    content: $("#updateRoleForm"),//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
                //删除该行
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //obj.del(); //删除对应行（tr）的DOM结构
                    //删除数据库中的数据
                    console.log(data)
                    var idList = [];
                    //遍历数组data，抽取所有的id
                    idList.push(data.roleId);
                    $.ajax({
                        url: "/role/deleteRole",
                        type: "delete",
                        dataType: "JSON",
                        data: {idList: idList},
                        success: function (ret) {
                            if (ret.code == 0) {
                                //刷新表格数据
                                roleTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                layer.msg(ret.msg, {icon: 6});
                            } else {
                                layer.msg(ret.msg, {icon: 5});
                            }
                        }
                    })
                    layer.close(index);
                    //向服务端发送删除指令
                });

            }
        });

        //获取选中的菜单id
        function getChecked_list(data) {
            let id = '';
            $.each(data, function (index, item) {
                if (id !== "") {
                    id = id + "," + item.id;
                } else {
                    id = item.id;
                }
                let i = getChecked_list(item.children);
                if (i !== "") {
                    id = id + "," + i;
                }
            })
            return id;
        }

        //addUserForm表单的提交
        form.on('submit(addRoleFilter)', function (data) {
            console.log("--------------------------")
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            let checkData = tree.getChecked('addFormMenu');
            let list = getChecked_list(checkData)
            //转成数组
            let menuList = list.split(",");
            //console.log(menuList);
            data.field.menus = list;
            //将状态属性的on转换为0，1
            if (data.field.status === "on") {
                data.field.status = 0;
            } else {
                data.field.status = 1;
            }
            //提交表单数据
            $.ajax({
                url: "/role/insertRole",
                type: "POST",
                dataType: "JSON",
                data: JSON.stringify(data.field),
                contentType: "application/json;charset=UTF-8",
                success: function (ret) {
                    if (ret.code == 0) {
                        //console.log(ret.data);
                        let id = ret.data.roleId;
                        saveorupdate(id,menuList);
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        roleTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }
                }
            })
            //将表单清空
            document.getElementById("addRoleForm").reset();
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //update表单的提交
        form.on('submit(updateRoleFilter)', function (data) {
                console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                let checkData = tree.getChecked('updateFormMenu');
                let list = getChecked_list(checkData)
                let menuList = list.split(",");
                data.field.menus = list;
                //将状态属性的on转换为0，1
                if (data.field.status === "on") {
                    data.field.status = 0;
                } else {
                    data.field.status = 1;
                }
                //提交表单数据
                $.ajax({
                    url: "/role/updateRole",
                    type: "PUT",
                    dataType: "JSON",
                    data: JSON.stringify(data.field),
                    contentType: "application/json;charset=UTF-8",
                    success: function (ret) {
                        if (ret.code == 0) {
                            let id = ret.data.roleId;
                            saveorupdate(id,menuList);
                            //关闭对话框
                            layer.closeAll();
                            //刷新表格数据
                            roleTable.reload({});
                            layer.msg(ret.msg, {icon: 6});
                        } else {
                            layer.msg(ret.msg, {icon: 5});
                        }
                    }
                })
                //将表单清空
                document.getElementById("updateRoleForm").reset();
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            }
        );

        //从后台查询菜单树格式后的数据，回显在表单中
        $.ajax({
            url: "/menu/selectAllMenus",
            type: "GET",
            dataType: "JSON",
            success: function (list) {
                //常规用法
                tree.render({
                    elem: '#addRoleTree' //默认是点击节点可进行收缩
                    , data: list.data
                    , showCheckbox: true
                    , id: 'addFormMenu'
                });

                tree.render({
                    elem: '#updateRoleTree' //默认是点击节点可进行收缩
                    , data: list.data
                    , showCheckbox: true
                    , id: 'updateFormMenu'
                });
            }
        })
        //批量修改和添加
        function saveorupdate(id,menuList) {
            var rolemenuList = [];
            for (let i = 0; i < menuList.length; i++) {
                let rolemenu = new Object();
                rolemenu.roleId = id;
                //console.log(id);
                rolemenu.menuId = menuList[i];
                //console.log(rolemenu.menuId);
                rolemenuList.push(rolemenu);
            }
            $.ajax({
                url: "/role/saveBatchRoleMenu",
                type: "POST",
                dataType: "JSON",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(rolemenuList),
                success: function (ret) {
                    if (ret.code == 0) {
                        //提交成功之后，怎么办?显示所有的测试题
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }
                }
            })
        }

    });
</script>
</body>
</html>